var canFormArray = function (arr, pieces) {
  let indexArr = [];
  for (let i = 0; i < pieces.length; i++) {
    for (let j = 0; j < arr.length; j++) {
      if (pieces[i][0] === arr[j]) {
        indexArr.push({ val: pieces[i], key: j });
        break;
      }
    }
  }
  indexArr.sort((a, b) => {
    return a.key - b.key;
  })
  let res = [];
  indexArr.forEach(item => {
    res.push(...item.val)
  })
  let i = 0;
  while (i < arr.length) {
    if (arr[i] !== res[i])
      return false;
    i++;
  }
  return true;
}
var canFormArray1 = function (arr, pieces) {
  let res = [];
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < pieces.length; j++) {
      if (arr[i] === pieces[j][0]) {
        res.push(...pieces[j])
      }
    }
  }
  let i = 0;
  while (i < arr.length) {
    if (arr[i] !== res[i])
      return false;
    i++;
  }
  return true;
}
var canFormArray2 = function (arr, pieces) {
  let res = [];
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < pieces.length; j++) {
      if (arr[i] === pieces[j][0]) {
        res.push(...pieces[j])
      }
    }
  }
  let i = 0;
  while (i < arr.length) {
    if (arr[i] !== res[i])
      return false;
    i++;
  }
  return true;
}
// console.log(canFormArray([15, 88], [[88], [15]]));
// console.log(canFormArray([49, 18, 16], [[16, 18, 49]]));
// console.log(canFormArray([91, 4, 64, 78], [[78], [4, 64], [91]]));
// console.log(canFormArray1([15, 88], [[88], [15]]));
// console.log(canFormArray1([49, 18, 16], [[16, 18, 49]]));
// console.log(canFormArray1([91, 4, 64, 78], [[78], [4, 64], [91]]));
// console.log(canFormArray2([15, 88], [[88], [15]]));
// console.log(canFormArray2([49, 18, 16], [[16, 18, 49]]));
// console.log(canFormArray2([91, 4, 64, 78], [[78], [4, 64], [91]]));
console.log(canFormArray2([3, 4, 8], [[3], [5, 8]]));